package com.igroupes.ovineherd.service.impl;

import com.igroupes.ovineherd.entity.DataModelTableEntity;
import com.igroupes.ovineherd.enums.ErrorCode;
import com.igroupes.ovineherd.exception.OvineHerdException;
import com.igroupes.ovineherd.raw.service.raw.DataModelAttributeService;
import com.igroupes.ovineherd.raw.service.raw.DataModelTableService;
import com.igroupes.ovineherd.service.AppService;
import com.igroupes.ovineherd.util.Requires;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AppServiceImpl implements AppService {
    @Autowired
    private DataModelTableService dataModelTableService;
    @Autowired
    private DataModelAttributeService dataModelAttributeService;

    @Override
    public void checkUserModel(Long userId, Long modelId) {
        Requires.requireNonNull(userId);
        Requires.requireNonNull(modelId);
        DataModelTableEntity modelTableEntity = dataModelTableService.getById(modelId);
        if (!modelTableEntity.getAddUser().equals(userId)) {
            throw new OvineHerdException(ErrorCode.PERMISSION_DENIED);
        }
    }

    @Override
    public void checkUserModelField(Long userId, Long fieldId) {
        DataModelTableEntity modelTableEntity = dataModelAttributeService.getByFieldId(fieldId);
        if (!modelTableEntity.getAddUser().equals(userId)) {
            throw new OvineHerdException(ErrorCode.PERMISSION_DENIED);
        }
    }


}
